<?php
/**
 * Created by PhpStorm.
 * User: jason
 * Date: 2017/11/4
 * Time: 18:03
 */

namespace Home\Service;


class ScoreFavoriteService
{
    /**
     * 获取用户是否已收藏谱单
     */
    public function haveFav($scoreId, $userId)
    {
        $map = array(
            'user_id' => $userId,
            'score_id' => $scoreId
        );
        $res = M('ScoreFavorite')->where($map)->find();
        if (!$res) {
            return false;
        }
        if ($res['is_delete'] == 1) {

            return false;
        }
        return true;
    }

    /**
     * 收藏/取消收藏谱单
     * @param $param
     * @return bool
     */
    public function favScore($param)
    {
        $map = array(
            'user_id' => $param['user_id'],
            'score_id' => $param['score_id']
        );
        $res = M('ScoreFavorite')->where($map)->find();
        if ($res) {
            if ($param['status'] == 0) {
                $param['is_delete'] = 1;
                unset($param['status']);
                $result = M('ScoreFavorite')->where($map)->save($param);
            } else {
                $param['is_delete'] = 0;
                unset($param['status']);
                $result = M('ScoreFavorite')->where($map)->save($param);
            }
        } else {
            $param['create_time'] = date('Y-m-d H:i:s', time());
            $result = M('ScoreFavorite')->add($param);
        }
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
}